const app = getApp();

Page({
  data: {
    realName: '',
    idCard: '',
    idCardFront: '',
    idCardBack: '',
    submitting: false
  },

  // 真实姓名输入
  onRealNameInput(e) {
    this.setData({
      realName: e.detail.value
    });
  },

  // 身份证号输入
  onIdCardInput(e) {
    this.setData({
      idCard: e.detail.value
    });
  },

  // 选择身份证正面
  chooseIdCardFront() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          idCardFront: res.tempFilePaths[0]
        });
      }
    });
  },

  // 选择身份证反面
  chooseIdCardBack() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          idCardBack: res.tempFilePaths[0]
        });
      }
    });
  },

  // 提交认证
  submitAuth() {
    if (!this.validateForm()) {
      return;
    }

    this.setData({
      submitting: true
    });

    wx.request({
        url: app.globalData.baseUrl + 'api/user-verify',
        method: 'POST',
        header: {
            'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        data: {
            "real_name":this.data.realName,
            "idcard":this.data.idCard,
        },
        success: res => {
            if (res.data.code === 0) {
                // 更新全局数据
                app.globalData.isRealNameAuth = true;
                if (app.globalData.userInfo) {
                    app.globalData.userInfo.isRealNameAuth = true;
                    wx.setStorageSync('userInfo', app.globalData.userInfo);
                }

                this.setData({
                    submitting: false
                });

                wx.showToast({
                    title: '认证成功',
                    icon: 'success'
                });

                // 返回上一页
                setTimeout(() => {
                    wx.navigateBack();
                }, 1500);
            } else {
                wx.showToast({
                    title: this.data.t.saveFail,
                    icon: 'none'
                })
            }
        },
        fail: () => {
            wx.showToast({
                title: this.data.t.saveFail,
                icon: 'none'
            })
        },
        complete: () => {
            this.setData({
                submitting: false
            });
        }
    })

  },

  // 验证表单
  validateForm() {
    if (!this.data.realName) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.idCard) {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      });
      return false;
    }

    if (!/^\d{17}[\dXx]$/.test(this.data.idCard)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.idCardFront) {
      wx.showToast({
        title: '请上传身份证正面',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.idCardBack) {
      wx.showToast({
        title: '请上传身份证反面',
        icon: 'none'
      });
      return false;
    }

    return true;
  }
}); 